package com.baidumap.administrator.happystudy.RestaurantFragement;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;

import com.baidumap.administrator.happystudy.base.ImgAdapter;
import com.baidumap.administrator.happystudy.base.JsonTools;
import com.baidumap.administrator.happystudy.base.MyGallery;
import com.baidumap.administrator.happystudy.base.MyListView;
import com.baidumap.administrator.happystudy.base.Utils;
import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.ui.RestauranFindActivity;
import com.baidumap.administrator.happystudy.adapter.RestauranAdapter;
import com.baidumap.administrator.happystudy.view.SwipyRefreshLayout;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class Fragment1 extends Fragment implements SwipyRefreshLayout.OnRefreshListener {

    @Bind(R.id.goback)
    LinearLayout goback;
    @Bind(R.id.find_img)
    ImageView findImg;


    private View view;
    private MyGallery gallery = null;
    private ArrayList<Integer> imgList;
    private ArrayList<ImageView> portImg;
    private int preSelImgIndex = 0;
    private LinearLayout ll_focus_indicator_container = null;

    public ImageLoader loader;
    private MyListView mListView;
    private ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    private RestauranAdapter mAdapter;
    private static int pageNo = 1;
    private ArrayList<Map<String, Object>> arrayList;

    private SwipyRefreshLayout mSwipyRefreshLayout;

    private ScrollView sv;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.rfg1, container, false);

        ll_focus_indicator_container = (LinearLayout) view
                .findViewById(R.id.ll_focus_indicator_container);
        imgList = new ArrayList<Integer>();
        boolean add = imgList.add(R.mipmap.lunbotu);
        imgList.add(R.mipmap.lunbotu);
        imgList.add(R.mipmap.lunbotu);
        InitFocusIndicatorContainer();
        gallery = (MyGallery) view.findViewById(R.id.gallery);
        gallery.setAdapter(new ImgAdapter(getActivity(), imgList));
        gallery.setFocusable(true);
        gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> arg0, View arg1,
                                       int selIndex, long arg3) {
                selIndex = selIndex % imgList.size();
                // 修改上一次选中项的背景
                portImg.get(preSelImgIndex).setImageResource(
                        R.mipmap.ic_focus);
                // 修改当前选中项的背景
                portImg.get(selIndex).setImageResource(
                        R.mipmap.ic_focus_select);
                preSelImgIndex = selIndex;
            }

            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });
        gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> info, View arg1,
                                    int position, long arg3) {
                position = position - 1;
                Toast.makeText(getActivity(), "position-->" + position,
                        Toast.LENGTH_SHORT).show();

            }

        });
        iniView();
        ButterKnife.bind(this, view);
        return view;
    }

    private void iniView() {
        sv = (ScrollView) view.findViewById(R.id.sv);
        sv.smoothScrollTo(0, 0);

        mSwipyRefreshLayout = (SwipyRefreshLayout) view.findViewById(R.id.main_SwipyRefreshLayout);
        // 设置分页第一页的索引 默认为0
        mSwipyRefreshLayout.setFirstIndex(pageNo);
        // 监听事件
        mSwipyRefreshLayout.setOnRefreshListener(this);
        // LoadImage初始化
        loader = ImageLoader.getInstance();
        pageNo = 1;
        mListView = (MyListView) view.findViewById(R.id.list);
        mAdapter = new RestauranAdapter(getActivity(), loader);
        new MyTask().execute("", "" + pageNo);// 线程加载json数据
    }

    @OnClick({R.id.goback, R.id.find_img})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.goback:
                getActivity().finish();
                break;
            case R.id.find_img:
                startActivity(new Intent(getActivity(), RestauranFindActivity.class));
                break;
        }
    }

    class MyTask extends
            AsyncTask<String, Void, ArrayList<Map<String, Object>>> {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

        }

        @Override
        protected ArrayList<Map<String, Object>> doInBackground(
                String... params) {

            ArrayList<Map<String, Object>> mList = null;
            mList = (ArrayList<Map<String, Object>>) JsonTools.listKeyMaps(
                    "list", Utils.HOUSE_JSON);
            return mList;
        }

        protected void onPostExecute(ArrayList<Map<String, Object>> result) {
            // TODO Auto-generated method stub

            list.addAll(result);//

            mAdapter.bindData(list);
            if (pageNo == 1) {

                mListView.setAdapter(mAdapter);
            }
            pageNo++;
            mAdapter.notifyDataSetChanged();
            super.onPostExecute(result);

        }
    }

    private void InitFocusIndicatorContainer() {
        portImg = new ArrayList<ImageView>();
        for (int i = 0; i < imgList.size(); i++) {
            ImageView localImageView = new ImageView(getActivity());
            localImageView.setId(i);
            ImageView.ScaleType localScaleType = ImageView.ScaleType.FIT_XY;
            localImageView.setScaleType(localScaleType);
            LinearLayout.LayoutParams localLayoutParams = new LinearLayout.LayoutParams(
                    24, 24);
            localImageView.setLayoutParams(localLayoutParams);
            localImageView.setPadding(5, 5, 5, 5);
            localImageView.setImageResource(R.mipmap.ic_focus);
            portImg.add(localImageView);
            this.ll_focus_indicator_container.addView(localImageView);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    /**
     * 下拉刷新
     *
     * @param index 当前分页索引
     */
    @Override
    public void onRefresh(int index) {
        list.clear();
        pageNo = 1;

        mSwipyRefreshLayout.setRefreshing(false);
    }

    /**
     * 加载更多
     *
     * @param index 当前分页索引
     */
    @Override
    public void onLoad(int index) {
        mSwipyRefreshLayout.setRefreshing(false);
    }
}
